package com.util;

import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class XMLExactractor {
	private Document doc=null;
public String[] getRecommendedPicSEEKImageUrls(String xmlString){
	ArrayList<String> picSEEKImageList=new ArrayList<String>();
	try{
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	DocumentBuilder db = dbf.newDocumentBuilder();
	InputSource is=new InputSource();
	is.setCharacterStream(new StringReader(xmlString));
	doc = db.parse(is);
	doc.getDocumentElement().normalize();
	}catch (Exception e) {
		// TODO: handle exception
		System.out.println("Error in creating document");
		System.out.println(e.getMessage());
	}
	int tempInt=1;
	Node picSEEkNode=doc.getElementsByTagName("picSEEKRecommendations").item(0);
	while(true){
		String tempStr="Image:"+tempInt;
		tempInt++;
		NodeList imageNodeList=((Element)picSEEkNode).getElementsByTagName(tempStr);
		if(imageNodeList.getLength()==0)
			break;
		Element tempImageNode=(Element)imageNodeList.item(0);
		Element urlNode=(Element)tempImageNode.getElementsByTagName("URL").item(0);
		NodeList urlValueList=urlNode.getChildNodes();
		picSEEKImageList.add(urlValueList.item(0).getNodeValue());
	}
	
	String[] urlList=new String[picSEEKImageList.size()];
	for(int i=0;i<picSEEKImageList.size();i++){
		urlList[i]=picSEEKImageList.get(i);
	}
	
	return urlList;
}

public String[] getSearchEngineRecommendedImsgeURLs(String xmlString){
	ArrayList<String> searchEngineImageList=new ArrayList<String>();
	try{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		InputSource is=new InputSource();
		is.setCharacterStream(new StringReader(xmlString));
		doc = db.parse(is);
		doc.getDocumentElement().normalize();
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("Error in creating document");
			System.out.println(e.getMessage());
		}
		int tempInt=1;
		Node searchEngineNode=doc.getElementsByTagName("SearchEngineRecommendations").item(0);
		while(true){
			String tempStr="Image:"+tempInt;
			tempInt++;
			NodeList imageNodeList=((Element)searchEngineNode).getElementsByTagName(tempStr);
			if(imageNodeList.getLength()==0)
				break;
			Element tempImageNode=(Element)imageNodeList.item(0);
			Element urlNode=(Element)tempImageNode.getElementsByTagName("URL").item(0);
			NodeList urlValueList=urlNode.getChildNodes();
			searchEngineImageList.add(urlValueList.item(0).getNodeValue());
		}
		String[] urlList=new String[searchEngineImageList.size()];
		for(int i=0;i<searchEngineImageList.size();i++){
			urlList[i]=searchEngineImageList.get(i);
		}
		
		
		return urlList;
}

public String[] getImageRankings(String xmlString){
	
	ArrayList<String> picSEEKImageRankings=new ArrayList<String>();
	try{
	DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
	DocumentBuilder db = dbf.newDocumentBuilder();
	InputSource is=new InputSource();
	is.setCharacterStream(new StringReader(xmlString));
	doc = db.parse(is);
	doc.getDocumentElement().normalize();
	}catch (Exception e) {
		// TODO: handle exception
		System.out.println("Error in creating document");
		System.out.println(e.getMessage());
	}
	int tempInt=1;
	Node picSEEkNode=doc.getElementsByTagName("picSEEKRecommendations").item(0);
	while(true){
		String tempStr="Image:"+tempInt;
		tempInt++;
		NodeList imageNodeList=((Element)picSEEkNode).getElementsByTagName(tempStr);
		if(imageNodeList.getLength()==0)
			break;
		Element tempImageNode=(Element)imageNodeList.item(0);
		Element urlNode=(Element)tempImageNode.getElementsByTagName("Rank").item(0);
		NodeList urlValueList=urlNode.getChildNodes();
		picSEEKImageRankings.add(urlValueList.item(0).getNodeValue());
	}
	
	
	String[] urlList=new String[picSEEKImageRankings.size()];
	for(int i=0;i<picSEEKImageRankings.size();i++){
		urlList[i]=picSEEKImageRankings.get(i);
	}
	
	
	
	return urlList;
}


public String[] getSrcURLs(String xmlString){
	ArrayList<String> SrcURLs=new ArrayList<String>();
	String[] SrcURLList=null;
	try{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		InputSource is=new InputSource();
		is.setCharacterStream(new StringReader(xmlString));
		doc = db.parse(is);
		doc.getDocumentElement().normalize();
		}catch (Exception e) {
			// TODO: handle exception
			System.out.println("Error in creating document");
			System.out.println(e.getMessage());
		}
		int tempInt=1;
		NodeList SrcNodeList=doc.getElementsByTagName("srcURL");
		if(SrcNodeList.getLength()!=0){
			int nodeListLength=SrcNodeList.getLength();
		for(int k=0;k<nodeListLength;k++){
			SrcURLs.add(SrcNodeList.item(k).getChildNodes().item(0).getNodeValue());
		}
		SrcURLList=new String[SrcURLs.size()];
		for(int i=0;i<SrcURLs.size();i++){
			SrcURLList[i]=SrcURLs.get(i);
		}
		
		
		
		}
	
	return SrcURLList;
}


}
